﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ext.Net;
using System.IO;
using ExamAnalysis.Core;
using ExamAnalysis.Model;

namespace ExamAnalysis.BasicInfo
{
    public partial class SchoolInfo : System.Web.UI.Page
    {
        static int schoolid = 0;
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void AddSchool_Click(object sender, Ext.Net.DirectEventArgs e)
        {
            this.FormWindow.Show();
            Button6.Hide();
            Button7.Show();
        }

        protected void UpdateSchool_Click(object sender, Ext.Net.DirectEventArgs e)
        {
            RowSelectionModel sm = this.SchoolInfoGridPanel.GetSelectionModel() as RowSelectionModel;
                foreach (SelectedRow row in sm.SelectedRows)
                {
                    schoolid = Convert.ToInt32(row.RecordID);
                }
                if (schoolid != 0)
                {
                    using (ExamAnalysisDataContext db = new ExamAnalysisDataContext())
                    {
                        var newschool = from s in db.SchoolInfo
                                        where s.SchoolID == schoolid
                                        select s;
                        ExamAnalysis.Model.SchoolInfo news = newschool.First();
                        FullName.Text = news.FullName;
                        AliasName.Text = news.AliasName;
                        Type.Text = news.Type;
                    }
                    this.FormWindow.Show();
                    Button6.Hide();
                    Button7.Show();
                }
            else
                {
                    X.Msg.Alert("提示信息", "请选择一个学校").Show();
                }
        }

        protected void DeleteSchool_Click(object sender, Ext.Net.DirectEventArgs e)
        {
             RowSelectionModel sm = this.SchoolInfoGridPanel.GetSelectionModel() as RowSelectionModel;
                foreach (SelectedRow row in sm.SelectedRows)
                {
                    schoolid = Convert.ToInt32(row.RecordID);
                }
                if (schoolid != 0)
                {
                    using (ExamAnalysisDataContext db = new ExamAnalysisDataContext())
                    {
                        var newschool = from s in db.SchoolInfo
                                        where s.SchoolID == schoolid
                                        select s;
                        ExamAnalysis.Model.SchoolInfo news = newschool.First();
                        db.SchoolInfo.DeleteOnSubmit(news);
                        db.SubmitChanges();
                        X.Msg.Alert("提示信息", "删除成功").Show();
                        Response.Redirect("SchoolInfo.aspx");
                    }
                }
                else
                {
                    X.Msg.Alert("提示信息", "请选择一个学校").Show();
                }
        }

        protected void Add_Click(object sender, Ext.Net.DirectEventArgs e)
        {
            ExamAnalysis.Model.SchoolInfo news = new ExamAnalysis.Model.SchoolInfo();
            news.FullName = FullName.Text;
            news.AliasName = AliasName.Text;
            news.Type = Type.Text; 
            using (ExamAnalysisDataContext db = new ExamAnalysisDataContext())
            {
                db.SchoolInfo.InsertOnSubmit(news);
                db.SubmitChanges();
                X.Msg.Alert("提示信息", String.Format("成功添加学校")).Show();
            }
            Response.Redirect("SchoolInfo.aspx");
        }
        protected void Change_Click(object sender, Ext.Net.DirectEventArgs e)
        {
            if (schoolid != 0)
            {
                using (ExamAnalysisDataContext db = new ExamAnalysisDataContext())
                {
                    var newschool = from s in db.SchoolInfo
                                    where s.SchoolID == schoolid
                                    select s;
                    ExamAnalysis.Model.SchoolInfo news = newschool.First();
                    news.FullName = FullName.Text;
                    news.AliasName = AliasName.Text;
                    news.Type = Type.Text;
                    news.SchoolID = schoolid;
                    db.SubmitChanges();
                    X.Msg.Alert("提示信息", String.Format("成功修改班级信息")).Show();
                    Response.Redirect("SchoolInfo.aspx");
                }
            }
            else
            {
                X.Msg.Alert("提示信息", "学校ID未获取，重新选择").Show();
            }
        }
    }
}